'use client'
import React, { useEffect, useState } from "react"
import './index.scss'
import { Modal, Button, message, Input } from 'antd'
import Upload from "@/app/components/upload/page";
import { DeleteWord, UpdateWord } from '@/app/api/RAGflow/index'
import keyStore from "@/app/store/key";


interface ModalProps {
    open: boolean;
    setData: (value: any) => void;
    id: string;
    type: string;
    data: any
}

const ModalUI: React.FC<ModalProps> = ({ open, setData, id, type, data }) => {

    const [title, settitle] = useState<string>('')
    const { RAGFlowKey } = keyStore()
    const [name, setname] = useState<string>('')
    useEffect(() => {
        if (type === 'edit') {
            settitle('修改文档')
        } else if (type === 'del') {
            settitle('删除文档')
        } else if (type === 'file') {
            settitle('上传文件')
        }
        setname(data.name)
    }, [data, type])
    return (
        <>
            <Modal open={open} className="modalui" title={title}
                footer={[
                    <>
                        {type !== 'file' && <Button onClick={() => {
                            if (type == 'del') {
                                DeleteWord(data.dataset_id, [data.id], RAGFlowKey).then((res: any) => {
                                    if (res.code == 0) {
                                        message.success('删除成功')
                                    } else {
                                        message.error('删除失败')
                                    }
                                    setData(false)
                                })
                            } else {
                                const obj = {
                                    name: name,
                                    chunk_method: data.chunk_method,
                                    parser_config: data.parser_config,
                                }
                                console.log(obj);
                                UpdateWord(data.dataset_id, data.id, obj, RAGFlowKey).then((res: any) => {
                                    if (res.code == 0) {
                                        message.success('修改成功')
                                    } else {
                                        message.error(res.message)
                                    }
                                    setData(false)
                                })
                            }
                        }}>确定</Button>}
                    </>
                ]} onCancel={() => { setData(false) }}
            >
                {type === 'file' && <Upload id={id} setdata={(value) => { setData(value) }} />}
                {type === 'del' && <div>确定删除吗？</div>}
                {type === 'edit' && <div>
                    <Input value={name} onChange={(e) => { setname(e.target.value) }} />
                </div>}
            </Modal>
        </>

    )
}


export default ModalUI